import Vue from 'vue'
import App from './App'
import Apis from '@/common/js/apis.js'
import Common from '@/common/js/common.js'
import DB from '@/common/js/db.js'
import Socket from '@/common/js/socket.js'
import Store from '@/common/js/store.js'
import Mh from '@/common/js/messageHandler.js'
import Icon from '@/common/js/icon.js'
import Pub from '@/common/js/public.js'

Vue.prototype.$store = {
	store: Store,
	mh: Mh,
	icon: Icon,
	sock: Socket,
	apis: Apis,
	common: Common,
	db: DB,
	pub: Pub
};
Vue.config.productionTip = false

String.prototype.replaceAll = function(exp, newStr) {
	return this.replace(new RegExp(exp, "gm"), newStr);
};

String.prototype.format = function(args) {
	let result = this;
	if (arguments.length < 1) {
		return result;
	}
	let data = arguments; // 如果模板参数是数组
	if (arguments.length == 1 && typeof(args) == "object") {
		// 如果模板参数是对象
		data = args;
	}
	for (let key in data) {
		let value = data[key];
		if (undefined != value) {
			if (value.toString().indexOf('{') > -1) {
				result = result.replace('{' + key + '}', value);
			} else {
				result = result.replaceAll("\\{" + key + "\\}", value);
			}
		}
	}
	return result;
}

Date.prototype.format = function(fmt = 'yyyy/MM/dd hh:mm:ss') {
	var o = {
		"M+": this.getMonth() + 1, //月份 
		"d+": this.getDate(), //日 
		"h+": this.getHours(), //小时 
		"m+": this.getMinutes(), //分 
		"s+": this.getSeconds(), //秒 
		"q+": Math.floor((this.getMonth() + 3) / 3), //季度 
		"S": this.getMilliseconds() //毫秒 
	};
	if (/(y+)/.test(fmt)) {
		fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
	}
	for (var k in o) {
		if (new RegExp("(" + k + ")").test(fmt)) {
			fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
		}
	}
	return fmt;
}

Promise.prototype.finally = function(callback) {
	let P = this.constructor;
	return this.then(
		value => P.resolve(callback()).then(() => value),
		reason => P.resolve(callback()).then(() => {
			throw reason
		})
	);
};

App.mpType = 'app'

const app = new Vue({
	...App
})
app.$mount()
uni.store = Store;
